# -*- coding: utf-8 -*-
# @Time : 2018/12/25 12:17
# @Author : Administrator
# @File : ws_requests.py
# @Project : 4.Python
# @title : 蜗牛进销存--requests库
import HTMLTestRunner
import requests,re,random
import unittest
class Woniusales_test(unittest.TestCase):
    def test_getSession(self):#获取登录的session
        self.ses = requests.session()# 实例化一个session
        data_login = {'username': 'admin', 'password': 'admin', 'verifycode': '0000'}
        self.ses.post(url='http://192.168.230.3:8080/woniusales/user/login',
                 data=data_login)
    def test_login001(self):#agileone登录，正确的用户名、正确的密码、正确的验证码
        datas={'username':'admin','password':'admin','verifycode':'0000'}
        req=requests.post(url='http://192.168.230.3:8080/woniusales/user/login',
                      data=datas)
        # print(req.text)
        assert re.search('login-pass',req.text)
    def test_login002(self):#agileone登录，错误的用户名、正确的密码、正确的验证码
        datas={'username':'adm','password':'admin','verifycode':'0000'}
        req=requests.post(url='http://192.168.230.3:8080/woniusales/user/login',
                      data=datas)
        # print(req.text)
        assert re.search('login-fail',req.text)
    def test_login003(self):#agileone登录，错误的用户名、正确的密码、空验证码
        datas={'username':'adm','password':'admin','verifycode':''}
        req=requests.post(url='http://192.168.230.3:8080/woniusales/user/login',
                      data=datas)
        # print(req.text)
        assert re.search('vcode-error',req.text)
    def test_addMemeber(self):#新增会员，所有输入合法
        index=random.randint(1000,9999)
        phone='1361234'+str(index)
        data_add={'customername':'Ploy','customerphone':phone,'childsex':'男','childdate':'2018-12-25','creditkids':'100','creditcloth':'300'}
        self.test_getSession()
        req=self.ses.post(url='http://192.168.230.3:8080/woniusales/customer/add',
                          data=data_add)
        # print(req.text)
        #所有输入合法，断言是否新增成功
        assert re.search('add-successful', req.text)
    def test_queryMemeber(self):#查询会员，所有输入合法
        data_query={'customerphone':'13612341234','page':'1'}
        self.test_getSession()
        req=self.ses.post(url='http://192.168.230.3:8080/woniusales/customer/search',
                          data=data_query)
        # print(req.text)
        assert '"customerphone":"13612341234"' in req.text #断言查询数据是否正确
    def test_uploadFile(self):#批次管理，上传文件
        self.test_getSession()
        index=random.randint(0,10000)
        bname='GB'+str(index)
        filename={'batchfile':('test.xls',open(r'D:\导入商品信息1.xls',mode='rb'))}
        req=self.ses.post(url='http://192.168.230.3:8080/woniusales/goods/upload',
                          data={'batchname':bname},
                          files=filename)
        # print(req.text)
        assert re.search('goodsid',req.text)   #断言是否上传成功
    def test_loadPic(self):#下载图片
        req=requests.get(url='http://192.168.230.3:8080/woniusales/image/logo.png')
        with open('D:\woniu.png',mode='wb') as file:
            file.write(req.content)

if __name__ == '__main__':
    suite=unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(Woniusales_test))
    # with open("test-report.xls","w",encoding='utf-8') as f:
    #     runner = unittest.TextTestRunner(stream=f, verbosity=2,descriptions="test report")
    #     runner.run(suite)
    with open("woniuSalesReport.html", "w", encoding="utf8") as file:
        runner = HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=1, title="进销存测试报告",
                                               description="requests 测试报告")
        runner.run(suite)

